#include "XQChatNetManager.h"
#include <QNetworkRequest>
#include <QNetworkReply>

using namespace XQChat;

CXQChatNetManager* XQChat::CXQChatNetManager::m_pNetManager = 0;

CXQChatNetManager* XQChat::CXQChatNetManager::getNetManager()
{
	if(!m_pNetManager)
		m_pNetManager = new CXQChatNetManager;

	return m_pNetManager;
}

XQChat::CXQChatNetManager::CXQChatNetManager( void )
	: m_pNetworkManager(0)
{
	m_pNetworkManager = new QNetworkAccessManager();
}

XQChat::CXQChatNetManager::~CXQChatNetManager( void )
{

}

bool XQChat::CXQChatNetManager::getRosterHeader( const QString& url, const QString& savefile )
{
	QEventLoop loop;
	QNetworkReply* pReply = m_pNetworkManager->get(QNetworkRequest(url));
	connect(pReply,SIGNAL(finished()),&loop,SLOT(quit()));
	loop.exec();

	if(pReply->error()!=QNetworkReply::NoError)
	{
		pReply->deleteLater();
		return false;
	}

	QFile file(savefile);
	if(!file.open(QFile::WriteOnly))
	{
		return false;
	}

	file.write(pReply->readAll());
	file.close();

	pReply->deleteLater();

	return true;
}
